package com.study.common.baidu.map;

import cn.hutool.http.HttpUtil;
import com.study.common.baidu.map.domain.LbsVo;
import com.study.common.constant.Constants;
import com.study.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class GoogleLbs {


    @Value("${map.google.api.url}")
    private String API_URL;

    @Value("${map.google.api.key}")
    private String API_KEY;

    @Value("${map.google.api.app.key}")
    private String API_APP_KEY;


    @Value("${map.google.api.languages}")
    private String languages;

    private static final String GOOGLE_STATUS_OK = "OK";

    private String DEFALT_LANGUAGE = " ";

    // 多国语言
    public static Map<String, String> langsMap = new HashMap<String, String>();

    @PostConstruct
    public void init() throws IOException {

        if (StringUtils.isNotBlank(languages)) {

            String[] ls = languages.split(",");

            for (String s : ls) {
                langsMap.put(s, s);
            }
        }

    }


    public String getLanguage(String language) {

        try {

            if(StringUtils.isBlank(language)) {
                language = DEFALT_LANGUAGE;
            }

//			if("zh_CN".equals(language)) {
//				language = "zh-Hans";
//			}else if("zh_TW".equals(language) || "zh_HK".equals(language)){
//				language = "zh-Hant";
//			}

            language = language.replace("_", "-");

            if(langsMap.containsKey(language)) {
                return langsMap.get(language);
            }else {

                String l = language.split("-")[0];
                if(langsMap.containsKey(l)) {
                    return langsMap.get(l);
                }

                return DEFALT_LANGUAGE;
            }

        } catch (Exception e) {
            return DEFALT_LANGUAGE;
        }

    }

    /**
     * 谷歌api物理地址转换
     * @param lng 经度
     * @param lat 纬度
     * @return 物理地址
     */
    public LbsVo getGoogleAddress(Double lng, Double lat, String language) {

        String address = Constants.STR_NULL;
        String locality = Constants.STR_NULL;
        try {

            language = getLanguage(language);

            String url = API_URL + "?latlng=" + lat + "," + lng + "&key=" + API_KEY + "&language="+language;


            String json = HttpUtil.get(url);



            //String json = HttpsUtil.httpsGetRequest(url,20000);
            if (StringUtils.isNotBlank(json)) {

                System.out.println(json);
                JSONObject obj = JSONObject.fromObject(json);
                String status = obj.getString("status");
                JSONArray addrList = obj.getJSONArray("results");

                if (GOOGLE_STATUS_OK.equalsIgnoreCase(status) && null != addrList && addrList.size() > 0) {
                    JSONObject addressObj = addrList.getJSONObject(0);
                    address = addressObj.getString("formatted_address");
                    locality = addressObj.getString("locality");
                }else {
                    log.error("GoogleLbs.getGoogleAddress.error={}",obj);
                }
            }
        } catch (Exception e) {
            log.error("GoogleLbs.getGoogleAddress.error={}", e);
        }
        LbsVo dto = new LbsVo();
        dto.setFormattedAddress(address);
        dto.setLocality(locality);
        return dto;
    }




}
